require "UIStepper"
YuanZhenShopScene={}
YuanZhenShopSceneFunc={}
YuanZhenShopSceneFunc.__index = YuanZhenShopSceneFunc
setmetatable(YuanZhenShopScene, YuanZhenShopSceneFunc)

YuanZhenShopSceneFunc.getNode = function()
  local l = {}
  setmetatable(l, YuanZhenShopSceneFunc)
  l.node = CCBReader:load("YuanZhenShopScene.ccbi")
  print("YuanZhenShopScene.getNode", l.node)
  YuanZhenShopScene.instance = l
  l:init() 
  return l 
end

YuanZhenShopSceneFunc.getScene = function()
  local sc = CCScene:create()
  sc:addChild(YuanZhenShopScene.getNode().node)
  print("YuanZhenShopScene.getScene", sc)
  return sc
end

YuanZhenShopSceneFunc.init = function(self)
  local tmp = string.format("我的战场积分：%d", UserData.userinfo.host.triggerHash.conSc)
  TypeCast(self.node:getChildByTag(1011), "CCLabelTTF"):setString(tmp)
  setObjectExtTable(self.node:getChildByTag(1111), self)
  --local NameConfig = {"levelstone2"="2级红宝石", "levelstone3"="3级红宝石", "wendingji1"="1级稳定剂", "expball"="经验球"}
  for i=1, 6 do
    local l = self.node:getChildByTag(2000+i)
    local info = UserData.config.models.ConquerGoods[i]
    local itemInfo = Item:createWithId(info.realId)
    print("YuanZhenShopSceneFunc.init", info.realId, itemInfo, itemInfo.itemConfig.asset)
    addSpriteFromBK(l, 1001, nameFromPath(itemInfo.itemConfig.asset))
    TypeCast(l:getChildByTag(1011), "CCLabelTTF"):setString(itemInfo.itemConfig.name)
    TypeCast(l:getChildByTag(1012), "CCLabelTTF"):setString(string.format("%d远征积分", info.score))
    TypeCast(l:getChildByTag(1013), "CCLabelTTF"):setString(string.format("x%d", info.num))
    
    local b = l:getChildByTag(1020)
    setObjectExtTable(b, self)
    b:setTag(i)
  end  
end
YuanZhenShopSceneFunc.onDuiHuanButton = function(self, node, event)
  local id = node:getTag()
  self.currentId = id
  NetworkRequest.conquerBuy(id, "onDuiHuanComplete", self)
  LoadScene:showLoadingScene()
end
YuanZhenShopSceneFunc.onDuiHuanComplete= function(self, data)
  LoadScene:removeLoadingScene()
  local res = cjson.decode(data)
  if res.ret==0 then
    NetworkRequest.syncData(res)
    local tmp = string.format("我的战场积分：%d", UserData.userinfo.host.triggerHash.conSc)
    TypeCast(self.node:getChildByTag(1011), "CCLabelTTF"):setString(tmp)

    local l = self.node:getChildByTag(2000+self.currentId)
    local x,y = l:getChildByTag(1001):getPosition()
    local info = UserData.config.models.ConquerGoods[self.currentId]
    local itemInfo = Item:createWithId(info.realId)
    local sp = CCSprite:create(nameFromPath(itemInfo.itemConfig.asset)) 
    sp:setPosition(l:convertToWorldSpace(ccp(x,y)))
    self.node:addChild(sp)

    local ma = CCMoveTo:create(0.5, ccp(20, 640))
    local ac = ActionUtil.createSeqAction({ma, CCCallFuncN:create(YuanZhenShopSceneFunc.onActionDone)})
    sp:runAction(ac)
  else
    showMessage(res.msg, ccc3(255,255,0))
  end
end
YuanZhenShopSceneFunc.onActionDone = function(node)
  node:getParent():removeChild(node, true)
end
YuanZhenShopSceneFunc.onClose = function(self, node, event)
  MainScene.scene:removeChild(node:getParent(), true)
  YuanZhenShopScene.instance = nil
  MainScene.scene:addChild(YuanZhenScene:getNode().node)  
end

